package com.xbongbong.workflow.model.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.workflow.domain.dao.WorkflowDao;
import com.xbongbong.workflow.domain.entity.WorkflowEntity;
import com.xbongbong.workflow.model.WorkflowModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 */
@Service("workflowModel")
public class WorkflowModelImpl implements WorkflowModel {
    @Resource
    private WorkflowDao workflowDao;

    @Override
    public Integer insert(WorkflowEntity entity) {
        long now = DateTimeUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);
        return workflowDao.insert(entity);
    }

    @Override
    public void insertBatch(List<WorkflowEntity> list) {
        workflowDao.insertBatch(list);
    }



    @Override
    public Integer update(WorkflowEntity entity) {
        entity.setUpdateTime(DateTimeUtil.getInt());
       return workflowDao.update(entity);
    }

    @Override
    public WorkflowEntity getByKey(Long key, String corpid) {
        return workflowDao.getByKey(key, corpid);
    }

    @Override
    public Integer deleteByKey(Long key, String corpid) {
        return workflowDao.deleteByKey(key, corpid);
    }

    @Override
    public Integer deleteBatch(List<Long> key, String corpid) {
        return workflowDao.deleteBatch(key, corpid);
    }

    @Override
    public List<WorkflowEntity> findEntitys(Map<String, Object> param) {
        return workflowDao.findEntities(param);
    }

    @Override
    public Integer updatePriority(List<WorkflowEntity> list, String corpid) {
        return workflowDao.updatePriority(list, corpid);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return workflowDao.getEntitiesCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public boolean isSaasWorkFlowEnable(String corpid, Integer businessType) {
        Map<String, Object> param = new HashMap<>();
        param.put(ParameterConstant.CORPID, corpid);
        param.put(StringConstant.SAAS_MARK, SaasMarkEnum.SAAS.getCode());
        param.put(ParameterConstant.BUSINESS_TYPE, businessType);
        param.put(ParameterConstant.DEL, DelEnum.NORMAL.getDel());
        param.put(ParameterConstant.ENABLE, BasicConstant.ONE);
        Integer entitysCount = getEntitysCount(param);
        if (entitysCount > 0) {
            return true;
        } else {
            return false;
        }
    }
}
